package com.heqiang.service.impl;


import com.heqiang.dao.UserDao;
import com.heqiang.dao.impl.UserDaoImpl;
import com.heqiang.domain.Asign;
import com.heqiang.domain.PageBean;
import com.heqiang.domain.User;
import com.heqiang.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public List<User> findAll() {

        return userDao.findAll();
    }

    @Override
    public Asign login(Asign asign) {
        Asign asign1 = userDao.login(asign);
        return asign1;

    }

    @Override
    public void addUser(User user) {
        userDao.add(user);
    }

    @Override
    public void delUser(String id) {
        userDao.del(id);
    }

    @Override
    public User findUser(String id) {
        return userDao.findUser(id);
    }

    @Override
    public void updateUser(User user) {
        userDao.update(user);
    }

    @Override
    public void delSelect(String[] uids) {
        for (String uid : uids) {
            userDao.del(uid);
        }
    }

    @Override
    public PageBean findUserByPage(String _currentPage, String _rows, String name, String address, String email) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        //创建空的PageBean对象
        PageBean<User> pb=new PageBean<User>();
        pb.setRows(rows);
        pb.setCurrentPage(currentPage);


        //调用dao查询总记录数
        int totalCount=userDao.findTotalCount(name,address,email);

        //System.out.println(totalCount);

        pb.setTotalCount(totalCount);
        //调用dao查询List集合
        //计算开始的记录索引
        int start=(currentPage-1)*rows;
        List<User> list=userDao.findByPage(start,rows,name,address,email);

        pb.setList(list);
        //计算总页码
        int totalPage=(totalCount%rows==0)?totalCount/rows:(totalCount/rows+1);
        pb.setTotalPage(totalPage);

        /*//先
        if (currentPage >= totalPage){
            currentPage=totalPage;
        }*/
        //后

        return pb;
    }
}
